package com.example.mdoncall;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

/**
 * 
 * @author Anthony Maniquiz & Laine Nicolas (Smart Telecom Inc. OJT) 
 * @version 1
 * 
 */

public class ToastActivity extends Activity {
	
	public void onCreate(Bundle b){
		super.onCreate(b);
		
		Intent intent = getIntent();
		String strNumber = intent.getStringExtra("search");
		
		int idOneP = 0;
		
		PersonDbHelper db = new PersonDbHelper(this);
		List<Contact> contacts = db.getAllContacts();
		
		String strFirstName = null;
		String strLastName = null;
		String strGender = null;
		//String strAllergy = null;
		String strCondition = null;
		String strMedication = null;
		
		String confirmNumber = null;
		int duplicatedCPnum = 0;
		
		for(Contact cn: contacts){
			if(cn.getPhoneNumber().equalsIgnoreCase(strNumber)){
				duplicatedCPnum += 1;
			}
		}
		
		if(duplicatedCPnum == 1){
		for(Contact cn: contacts) {
			if(cn.getPhoneNumber().equalsIgnoreCase(strNumber)){
				idOneP = cn.getID();
				strFirstName = cn.getFname();
				strLastName = cn.getLname();
				strGender = cn.getGender();
				//strAllergy = cn.getAllergy();
				strCondition = cn.getCondition();
				strMedication = cn.getMedication();
				confirmNumber = cn.getPhoneNumber();
			}
		}
		
		if(strNumber.equals(confirmNumber)) {
		
		String strData = "Name: " + strFirstName + " " + strLastName + "\n\nGender: " + strGender + 
						"\n\nCondition: " + strCondition + "\n\nMedication: " + strMedication;
		
		Intent intentTwo = new Intent(this, ViewPatientActivity.class);
		intentTwo.putExtra("search", idOneP);
			
			for(int i = 0; i < 3; i++){
				Toast.makeText(getApplicationContext(), strData, Toast.LENGTH_SHORT).show();
				startActivity(intentTwo);
				}
		
		}}if(duplicatedCPnum > 1){
			
			String strData = "";
			
			for(Contact cn: contacts){
				if(cn.getPhoneNumber().equalsIgnoreCase(strNumber)){
					strData += "Name: "+cn.getLname()+", "+cn.getFname()+"\nGender: "+cn.getGender()+"\n\n";
				}
			}
			
			Intent intentTwo = new Intent(this, RelatedPatientsActivity.class);
			intentTwo.putExtra("search", strNumber);
			
			for(int i = 0; i < 3; i++){
				Toast.makeText(getApplicationContext(), strData, Toast.LENGTH_SHORT).show();
				startActivity(intentTwo);
				}
			
		} else if(duplicatedCPnum==0){
			Toast.makeText(getApplicationContext(), "Caller is not Registered in MD On Call ", Toast.LENGTH_LONG).show();
			Intent intentThree = new Intent(this, PatientInfoActivity.class);
			intentThree.putExtra("callerNumber", strNumber);
			startActivity(intentThree);
			}
		
		db.close();
		
		finish();
	}
}

